<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('回款时间')" prop="receivableDate">
            <el-date-picker
              v-model="form.receivableDate"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('回款时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('回款金额(元)')" prop="receivableAmount">
            <Money v-model="form.receivableAmount" :placeholder="$t('回款金额(元)')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('回款主体')" prop="investorId">
            <el-select v-model="form.investorId" filterable :placeholder="$t('回款主体')">
              <el-option
                v-for="investment in investmentList"
                :key="investment.id"
                :label="investment.investorName"
                :value="investment.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('SPV')" prop="spvId">
            <spv-select v-model="form.spvId" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('回款类别')" prop="receivableType">
            <dict-select
              v-model="form.receivableType"
              dictionary="deal_estate_collection_type"
              :placeholder="$t('回款类别')"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="deal:receivable" />
    </div>
  </div>
</template>

<script>
    import { saveDealExpectReceivable, getDealExpectReceivable } from '@/api/deal/estate/receivable/dealExpectReceivable'
    import SpvSelect from '@/components/Select/SpvSelect.vue'
    import { listDealInvestment } from '@/api/deal/investment/dealInvestment'
    export default {
      components: {
        SpvSelect
      },
        data() {
            return {
                // 表单参数
                form: {},
                // 遮罩层
                loading: false,
                receivableTypeOptions: [],
                // 项目投资主体列表
                investmentList: [],
                // 表单校验
                rules: {
                receivableDate: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                receivableAmount: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                investorId: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                receivableType: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ]
                }
            }
        },
        created() {
          this.$bus.$emit('async_done')
          listDealInvestment({ 'dealId': this.$route.query.id }).then(response => {
            this.investmentList = response.rows
          })
            this.getDicts('deal_estate_collection_type').then(response => {
                this.receivableTypeOptions = response.data
            })
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getDealExpectReceivable(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,

                    dealId: this.$route.query.id,

                    receivableDate: undefined,

                    receivableAmount: undefined,

                    investorId: undefined,

                    spvId: undefined,

                    receivableType: undefined,

                    createBy: undefined,

                    createTime: undefined,

                    updateBy: undefined,

                    updateTime: undefined,

                    delFlag: undefined

                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveDealExpectReceivable(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                       this.locationError(document)
                    }
                })
           }

        }

    }
</script>
